﻿using FireChat.Client.Model.Message;
using FireChat.Client.Model.Message.Concrete;
using FireChat.Client.Session;
using FireChat.Client.Session.Manager;
using FireChat.Client.Session.Receiver;
using FireChat.Share.Command;
using FireChat.Share.Logger;
using FireChat.Share.Web.Model.Enums;

namespace FireChat.Client;


/// <summary>
/// 客户端 控制台
/// </summary>
internal class ClientConsole
{
    /// <summary>
    /// 指令映射表
    /// </summary>
    public static readonly CommandMap<ClientConsole> CommandMap = new();

    /// <summary>
    /// 客户端 核心
    /// </summary>
    private static ClientCore ClientCore = Restart();



    /// <summary>
    /// 客户端 控制台
    /// </summary>
    public static void Main()
    {
        Console.Title = "FireChat Client";
        CommandMap.Start();
    }


    #region --指令区--

    [CommandToken("restart", "重启")]
    private static ClientCore Restart()
    {
        $"正在启动客户端核心...".LogToConsole();

        try
        {
            ClientCore client_core = new("ws://localhost:23333/");
            client_core.PackageReceiver += receiver =>
            {
                receiver.ExcuteAction();
            };
            client_core.LaunchAsync().GetAwaiter().GetResult();

            try { ClientCore?.CloseAsync(); } catch { }

            ClientCore = client_core;
            return ClientCore;
        }
        catch(Exception ex)
        {
            ex.LogToConsole();
            return ClientCore;
        }
    }

    [CommandToken("stop", "exit", "退出", "Close")]
    public static async void Close()
    {
        await ClientCore.CloseAsync();
        "已关闭".LogToConsole();

        Environment.Exit(0);
    }


    [CommandToken("clr", "clear", "清屏")]
    public static void ClearScreen()
    {
        Console.Clear();
    }

    
    [CommandToken("login", "登录")]
    public static async void Login(string username, string password)
    {
        var resp = await AccountManager.LoginAsync(ClientCore, username, password);
        resp.Message.LogToConsole();
    }


    [CommandToken("register", "注册")]
    public static async void Register(string username, string password)
    {
        var resp = await AccountManager.Register(ClientCore, username, password);
        resp.Message.LogToConsole();
    }

    [CommandToken("logout", "注销", "登出")]
    public static async void Logout()
    {
        var resp = await AccountManager.LogoutAsync(ClientCore);
        resp.Message.LogToConsole();
    }


    [CommandToken("ApplyFriend", "申请好友")]
    public static async void ApplyFriend(string targetUsername)
    {
        var resp = await ClientCore.ApplyFriendsAsync(targetUsername);
        resp.Message.LogToConsole();
    }

    [CommandToken("DeleteFriend", "删除好友")]
    public static async void DeleteFriend(string targetUsername)
    {
        var resp = await ClientCore.DeleteFriendAsync(targetUsername);
        resp.Message.LogToConsole();
    }

    [CommandToken("AgreeFriend", "同意好友")]
    public static async void AgreeFriend(string targetUsername)
    {
        var resp = await ClientCore.AgreeFriendAsync(targetUsername);
        resp.Message.LogToConsole();
    }


    [CommandToken("FriendMessage", "好友消息")]
    public static async void FriendMessage(string targetUsername, string messages)
    {
        MessageChain chain = new()
        {
            new TextMessage() { Text = string.Join(" ", messages) }
        };

        var resp = await ClientCore.SendFriendMessageAsync(targetUsername, chain);
        resp.Message.LogToConsole();
    }
    

    #endregion
}
